$(function() {

    // jquery.validate 自定义校验
    jQuery.validator.addMethod("validEnv", function(value, element) {
        var length = value.length;
        var valid = /^[a-z]*$/;
        return this.optional(element) || valid.test(value);
    }, "限制由小写字母组成");

    $('.config').on('click', function(){
        $.post(base_url + "/alarm/update",  $("#alarm_form").serialize(), function(data, status) {
            if (data.status == 200) {

                layer.open({
                    icon: '1',
                    content: '操作成功' ,
                    end: function(layero, index){
                        window.location.reload();
                    }
                });

            } else {
                layer.open({
                    icon: '2',
                    content: (data.message||'操作失败')
                });
            }
        });
    });
    var addModalValidate = $("#addModal .form").validate({
        errorElement : 'span',
        errorClass : 'help-block',
        focusInvalid : true,
        rules : {
            env : {
                required : true,
                rangelength:[3,50],
                validEnv : true
            },
            title : {
                required : true,
                rangelength:[4, 100]
            },
            order : {
                digits:true
            }
        },
        messages : {
        },
        highlight : function(element) {
            $(element).closest('.form-group').addClass('has-error');
        },
        success : function(label) {
            label.closest('.form-group').removeClass('has-error');
            label.remove();
        },
        errorPlacement : function(error, element) {
            element.parent('div').append(error);
        },
        submitHandler : function(form) {
            $.post(base_url + "/alarm/save",  $("#addModal .form").serialize(), function(data, status) {
                if (data.status == 200) {
                    $('#addModal').modal('hide');

                    layer.open({
                        icon: '1',
                        content: '操作成功' ,
                        end: function(layero, index){
                            window.location.reload();
                        }
                    });

                } else {
                    layer.open({
                        icon: '2',
                        content: (data.message||'操作失败')
                    });
                }
            });
        }
    });
    $("#addModal").on('hide.bs.modal', function () {
        $("#addModal .form")[0].reset();
        addModalValidate.resetForm();
        $("#addModal .form .form-group").removeClass("has-error");
    });

    $('.update').on('click', function(){
        $("#updateModal .form input[name='grayId']").val($(this).attr("grayId"));
        $("#updateModal .form textarea[name='ipList']").val($(this).attr("ipList"));
        $("#updateModal .form textarea[name='rawContent']").val($(this).attr("rawContent"));

        $('#updateModal').modal({backdrop: false, keyboard: false}).modal('show');
    });
    var updateModalValidate = $("#updateModal .form").validate({
        errorElement : 'span',
        errorClass : 'help-block',
        focusInvalid : true,
        rules : {
            env : {
                required : true,
                rangelength:[3,50],
                validEnv : true
            },
            title : {
                required : true,
                rangelength:[4, 100]
            },
            order : {
                digits:true
            }
        },
        messages : {
        },
        highlight : function(element) {
            $(element).closest('.form-group').addClass('has-error');
        },
        success : function(label) {
            label.closest('.form-group').removeClass('has-error');
            label.remove();
        },
        errorPlacement : function(error, element) {
            element.parent('div').append(error);
        },
        submitHandler : function(form) {
            $.post(base_url + "/gray/update",  $("#updateModal .form").serialize(), function(data, status) {
                if (data.status == 200) {
                    $('#addModal').modal('hide');

                    layer.open({
                        icon: '1',
                        content: '更新成功' ,
                        end: function(layero, index){
                            window.location.reload();
                        }
                    });
                } else {
                    layer.open({
                        icon: '2',
                        content: (data.message||'更新失败')
                    });
                }
            });
        }
    });
    $("#updateModal").on('hide.bs.modal', function () {
        $("#updateModal .form")[0].reset();
        addModalValidate.resetForm();
        $("#updateModal .form .form-group").removeClass("has-error");
    });


	
});
